package com.example.clientmiviamoviego4;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class MyParser{

	/**
	 * Oggetto che si occupa della lettura della risposta del server,
	 * effettuando le operazioni di parsing da un XML.
	 * */
	private XmlPullParserFactory xmlFactoryObject;

	/**
	 * Oggetto indicante l'operazione da effettuare all'atto di una richiesta al
	 * server.
	 * */
	private OperationEnum operation;

	/**
	 * ArrayList che raccoglie i risultati di una richiesta al server.
	 * */
	private ArrayList<Object> result;
	private String xmlResult;

	/**
	 * Stringa indicante l'URL base del server, dove viene indicato l'indirizzo
	 * IP e la porta di ascolto del server.
	 * */
	public String basePath;



	/**
	 * Costruttore della classe MyParser
	 * 
	 * @param basePath
	 * 			Indirizzo IP e la porta di ascolto del server.
	 */
	public MyParser(String basePath){
		this.basePath = basePath;
		this.result = new ArrayList<Object>();
		this.xmlResult = new String("");
	}

	/**
	 * Metodo che gestisce lo switch tra le varie operazioni da effettuare
	 * all'atto della connessione al server.
	 * @throws XmlPullParserException 
	 * */
	public void parseXMLAndStoreIt(InputStream stream, OperationEnum operation) throws XmlPullParserException {

		xmlFactoryObject = XmlPullParserFactory.newInstance();
		XmlPullParser myParser = xmlFactoryObject.newPullParser();
		myParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
		myParser.setInput(stream, null);

		if (operation.equals(OperationEnum.GET_GENRES)
				|| operation.equals(OperationEnum.GET_USER_GENRES)) {
			getGenresFromXML(myParser);
			
		} else if (operation.equals(OperationEnum.GET_SEARCH_MOVIES)
				|| operation.equals(OperationEnum.GET_USER_NEWS)
				|| operation.equals(OperationEnum.GET_USER_MOVIES)) {
			getMoviesFromXML(myParser);

		} else if (operation.equals(OperationEnum.GET_MOVIE_FROM_ID)) {
			getMovieFromIdFromXML(myParser);

		}

	}

	public String getXmlFromObject(Object object, OperationEnum operation){
			if (operation.equals(OperationEnum.PUT_USER)){
				User user = (User) object;
				xmlResult = getXMLFromUser(user);
			
			}
			else if(operation.equals(OperationEnum.POST_USER_GENRES)){
				ArrayList<ParcelableMovieType> listMovies = (ArrayList<ParcelableMovieType>)object;
				
				xmlResult = getXMLFromListMovieType(listMovies);
	
			}
			
			return xmlResult;
	}

	/**
	 * Metodo di parsing da XML a ParcelableMovieType
	 * */
	private void getGenresFromXML(XmlPullParser myParser) {
		int event;
		String text = null;

		try {
			event = myParser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				String name = myParser.getName();
				switch (event) {
				case XmlPullParser.START_TAG:
					break;
				case XmlPullParser.TEXT:
					text = myParser.getText();
					break;

				case XmlPullParser.END_TAG:
					if (name.equals("key")) {
						result.add(new ParcelableMovieType(text));
					}
					break;
				}
				event = myParser.next();

			}

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * Metodo di parsing da XML a ParcelableMovie
	 * */
	private void getMoviesFromXML(XmlPullParser myParser) {
		int event;
		String text = null;
		String date = null;
		String id = null;
		String thumbnailURL = null;
		String title = null;

		try {
			event = myParser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				String name = myParser.getName();
				switch (event) {
				case XmlPullParser.START_TAG:
					break;
				case XmlPullParser.TEXT:
					text = myParser.getText();
					break;

				case XmlPullParser.END_TAG:
					if (name.equals("date")) {
						date = text;
					} else if (name.equals("id")) {
						id = text;
					} else if (name.equals("thumbnailURL")) {
						thumbnailURL = basePath + text;
					} else if (name.equals("title")) {
						title = text;
						result.add(new ParcelableMovie(id, title, date,
								thumbnailURL));
					}
					break;
				}

				event = myParser.next();

			}

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * Metodo di parsing da XML ad ArrayList di ParcelableDetailedMovie
	 * */
	private void getMovieFromIdFromXML(XmlPullParser myParser) {
		int event;
		String text = null;
		String date = null;
		String director = null;
		String id = null;
		String playbillURL = null;
		String thumbnailURL = null;
		String trailerURL = null;
		String title = null;
		ArrayList<String> actors = new ArrayList<String>();
		ArrayList<ParcelableMovieType> movieTypes = new ArrayList<ParcelableMovieType>();

		try {
			event = myParser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				String name = myParser.getName();
				switch (event) {
				case XmlPullParser.START_TAG:
					break;
				case XmlPullParser.TEXT:
					text = myParser.getText();
					break;

				case XmlPullParser.END_TAG:
					if (name.equals("actors")) {
						actors.add(text);
					} else if (name.equals("date")) {
						date = text;
					} else if (name.equals("director")) {
						director = text;
					} else if (name.equals("id")) {
						id = text;
					} else if (name.equals("key")) {
						movieTypes.add(new ParcelableMovieType(text));
					} else if (name.equals("playbillURL")) {
						playbillURL = basePath + text;
					} else if (name.equals("thumbnailURL")) {
						thumbnailURL = basePath + text;
					} else if (name.equals("title")) {
						title = text;
					} else if (name.equals("trailerURL")) {
						trailerURL = basePath + text;
						result.add(new ParcelableDetailedMovie(id, title,
								director, date, actors, movieTypes,
								thumbnailURL, playbillURL, trailerURL));
					}

					break;
				}

				event = myParser.next();

			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * Metodo di parsing da User a XML
	 * */
	public  String getXMLFromUser(User user){

		StringBuilder result = new StringBuilder();

		result.append("<user><id>");
		result.append(user.getId());
		result.append("</id>");

		for (ParcelableMovieType singleType : user.getMovieTypes()) {
			result.append("<movieTypes><key>");
			result.append(singleType.getKey());
			result.append("</key></movieTypes>");
		}

		result.append("</user>");
		
		return result.toString();
	}

	/**
	 * Metodo di parsing da ArrayList di ParcelableMovieType a XML
	 * */
	public  String getXMLFromListMovieType(
			ArrayList<ParcelableMovieType> listMovies) {

		StringBuilder result = new StringBuilder();

		result.append("<movieTypes>");

		for (ParcelableMovieType singleType : listMovies) {
			result.append("<movieType><key>");
			result.append(singleType.getKey());
			result.append("</key></movieType>");
		}

		result.append("</movieTypes>");
		
		return result.toString();
	}

	public Object getResult(){
		return result;
	}
}
